//
//  WBWelcomeView.swift
//  新浪微博
//
//  Created by 张甜甜 on 2017/11/29.
//  Copyright © 2017年 sweet. All rights reserved.
//

import UIKit
import SDWebImage
//欢迎界面
class WBWelcomeView: UIView {
    private lazy var iconImageView:UIImageView = UIImageView()
    private lazy var tipLabel:UILabel = UILabel()
    override init(frame: CGRect) {
        super .init(frame: frame)
        backgroundColor = UIColor .clear
        setUPui()
        setData()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("错误信息")
    }
    override func layoutSubviews() {
        super .layoutSubviews()
        UIView .animate(withDuration: 2.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            //自动布局的动画 更新frame
            self.iconImageView .frame = CGRect(x: self.iconImageView.frame.origin.x, y: 100, width: 86, height: 86)
            self.tipLabel.frame = CGRect(x: self.iconImageView.frame.origin.x, y: self.iconImageView.frame.origin.y+self.iconImageView.frame.size.height+10, width: self.iconImageView.frame.size.width, height: 20)
        }) { (_) in
            UIView .animate(withDuration: 4.0, animations: {
                self.tipLabel.alpha = 1;
            }, completion: { (_) in
                self .removeFromSuperview()
            })
        }
    }
    

}
extension WBWelcomeView {
    private func setUPui(){
        let xPoint = (UIScreen .cz_screenWidth() - 86)/2
        let yPoint = UIScreen .cz_screenHeight()
        
        let backImageView:UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: (UIScreen .cz_screenWidth()), height: yPoint))
        backImageView.image = UIImage(named: "ad_background")
        self.addSubview(backImageView)
        
        iconImageView.frame = CGRect(x:xPoint, y: yPoint-200, width: 86, height: 86)
        iconImageView.image = UIImage(named: "avatar_default_big")
        self .addSubview(iconImageView)
        
        tipLabel = UILabel(frame: CGRect(x: iconImageView.frame.origin.x, y: iconImageView.frame.origin.y+iconImageView.frame.size.height+10, width: iconImageView.frame.size.width, height: 20))
        tipLabel .alpha = 0
        tipLabel.text = "欢迎回来"
        tipLabel.textAlignment = .center
        tipLabel.font = UIFont .systemFont(ofSize: 15)
        self .addSubview(tipLabel)
    }
}

extension WBWelcomeView {
    private func setData(){
        guard let urlStr = WBNetworkManager .shared .userAccount .avatar_large,let url = URL(string: urlStr) else {
            return
        }
        iconImageView .sd_setImage(with: url, placeholderImage: UIImage(named: "avatar_default_big"))
//        clipsToBounds 针对view
        iconImageView .layer .masksToBounds = true
        iconImageView .layer .cornerRadius = 43.0
        
    }
}





